package com.lipaluma.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * Utility class for File
 * @author mario.soares
 */
public class FileUtils {
	
	/**
	 * Permits return {@link InputStream} from a file path.<p>
	 * If 'classpath:' is mentioned on prefix in the path, the method will search in the classpath of the project
	 * @param filepath
	 * @return the inputStream
	 * @throws FileNotFoundException
	 */
	public static InputStream getInputStreamFromFilePath(String filepath) throws FileNotFoundException {
		InputStream inputStream;
		if (filepath.startsWith("classpath:")) {
			inputStream = ClassLoader.getSystemResourceAsStream(filepath.substring("classpath:".length()));
			if (inputStream == null)
				throw new FileNotFoundException("file " + filepath + " not found ");
		} else {
			inputStream = new FileInputStream(filepath);
		}
		return inputStream;
	}
}
